package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.xiuxian.config.*;
import com.yanqu.road.entity.activity.xiuxian.data.*;
import com.yanqu.road.entity.activity.xiuxian.config.XiuXianConfig;
import com.yanqu.road.entity.activity.xiuxian.config.XiuXianPvpPlaceConfig;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianPveUserProperty;
import com.yanqu.road.entity.enums.xiuxian.eXiuXianType;
import com.yanqu.road.pb.activity.FellingTreesProto;
import com.yanqu.road.pb.activity.XiuXianProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 凡人修仙
 */
public class XiuXianPb {

    public static XiuXianProto.XiuXianUserDataTemp.Builder parseUserData(XiuXianUserData userData, Map<String, XiuXianUserBehaviorData> behaviorMap,
                                                                         Map<Integer, XiuXianMissionData> missionMap) {
        if (null == userData) {
            return null;
        }
        XiuXianProto.XiuXianUserDataTemp.Builder builder = XiuXianProto.XiuXianUserDataTemp.newBuilder();
        builder.setEnergy(userData.getEnergy());
        builder.setAngerVal(userData.getAngerVal());
        builder.setBreakCount(userData.getBreakCount());
        builder.setLastBreakFailTime(userData.getLastBreakFailTime());
        builder.setLastEnergyRecTime(userData.getLastEnergyRecTime());
        builder.setLastMeditateTime(userData.getLastMeditateTime());
        builder.setPracticeSpeed(userData.getPracticeSpeed());
        builder.setPowerVal(userData.getPowerVal());
        builder.setMaxPowerVal(userData.getMaxPowerVal());
        builder.setStateId(userData.getStateId());
        builder.setTripodLv(userData.getTripodLv());
        builder.setTripodExp(userData.getTripodExp());
        builder.setPracticeVal(userData.getPracticeVal());
        builder.setMountId(userData.getMountId());
        builder.setImageId(userData.getImageId());
        builder.setLuckPoint(userData.getLuckPoint());
        builder.setUnreceivedLuckPoint(userData.getUnreceivedLuckPoint());
        builder.addAllItem(parseXiuXianItemMsgList(userData.getItems()));
        builder.setDrawTimes(userData.getDrawTimes());
        if (null != userData.getEquipmentMap()) {
            for (XiuXianEquipmentData data : userData.getEquipmentMap().values()) {
                XiuXianProto.EquipmentInfoTemp.Builder parseEquipmentInfo = parseEquipmentInfoTemp(data);
                if(parseEquipmentInfo != null) {
                    builder.addEquipmentList(parseEquipmentInfo);
                }
            }
        }
        builder.setProperty(parseXiuXianPveUserPropertyTemp(userData.getProperty()));
        builder.setTripodEquipment(parseEquipmentInfoTemp(userData.getTripodEquipment()));

        // 地图历练行为统计
        if (null != behaviorMap) {
            for (XiuXianUserBehaviorData data : behaviorMap.values()) {
                XiuXianProto.MissionBehaviorStatTemp.Builder parseInfo = parseMissionBehaviorTemp(data);
                if(parseInfo != null) {
                    builder.addBehaviorDataList(parseInfo);
                }
            }
        }
        // 地图数据
        if (null != missionMap) {
            for (XiuXianMissionData data : missionMap.values()) {
                XiuXianProto.MissionInfoTemp.Builder parseInfo = parseMissionInfoTemp(data);
                if(parseInfo != null) {
                    builder.addMissionInfoList(parseInfo);
                }
            }
        }
        builder.setSectId(userData.getSectId());


        return builder;
    }

    public static XiuXianProto.EquipmentInfoTemp.Builder parseEquipmentInfoTemp(XiuXianEquipmentData testData){
        if(null == testData){
            return null;
        }
        XiuXianProto.EquipmentInfoTemp.Builder msg =  XiuXianProto.EquipmentInfoTemp.newBuilder();
        msg.setEquipmentId(testData.getEquipmentId());
        msg.setBaseLv(testData.getBaseLv());
        msg.setAdvanceLv(testData.getAdvanceLv());
        msg.setSkillLv(testData.getSkillLv());
        msg.setDiffPowerVal(testData.getDiffPowerVal());
        for (Map.Entry<Integer, Long> entry : testData.getAttrAdvanceMap().entrySet()) {
            msg.addAttrAdvanceList(parseXiuXianAttrInfoTemp(entry.getKey(), entry.getValue()));
        }
        return msg;
    }

    public static XiuXianProto.MissionInfoTemp.Builder parseMissionInfoTemp(XiuXianMissionData testData){
        if(null == testData){
            return null;
        }
        XiuXianProto.MissionInfoTemp.Builder msg =  XiuXianProto.MissionInfoTemp.newBuilder();
        msg.setMissionId(testData.getMissionId());
        msg.setIsFinish(testData.getIsFinish());
        if(testData.getHasExploreGridList() != null) {
            for(XiuXianGridPositionData data : testData.getHasExploreGridList()){
                XiuXianProto.GridPositionTemp.Builder tempMsg = parseGridPositionTemp(data);
                if(null != tempMsg){
                    msg.addHasExploreGridList(tempMsg);
                }
            }
        }
        if(testData.getSubsectionGetList() != null) {
            for(int subsectionVal : testData.getSubsectionGetList()){
               msg.addSubsectionGetList(subsectionVal);
            }
        }
        return msg;
    }

    public static XiuXianProto.MissionBehaviorStatTemp.Builder parseMissionBehaviorTemp(XiuXianUserBehaviorData testData){
        if(null == testData){
            return null;
        }
        XiuXianProto.MissionBehaviorStatTemp.Builder msg =  XiuXianProto.MissionBehaviorStatTemp.newBuilder();
        msg.setMissionId(testData.getMissionId());
        msg.setBehaviorType(testData.getBehaviorType());
        msg.setBehaviorParam(testData.getBehaviorParam());
        msg.setCount(testData.getCount());
        return msg;
    }

    public static XiuXianProto.GridPositionTemp.Builder parseGridPositionTemp(XiuXianGridPositionData testData){
        if(null == testData){
            return null;
        }
        XiuXianProto.GridPositionTemp.Builder msg =  XiuXianProto.GridPositionTemp.newBuilder();
        msg.setX(testData.getX());
        msg.setY(testData.getY());
        msg.setFinishStatus(testData.getFinishStatus());
        return msg;
    }

    public static XiuXianProto.XiuXianConfigSyncMsg.Builder parseXiuXianConfigSyncMsg(XiuXianConfig config, String language) {
        XiuXianProto.XiuXianConfigSyncMsg.Builder builder =  XiuXianProto.XiuXianConfigSyncMsg.newBuilder();
        for (XiuXianPvpPlaceConfig placeConfig : config.getPlaceConfigMap().values()) {
            builder.addPlaceConfig(parseXiuXianPvpPlaceConfigTemp(placeConfig, language));
        }
        for (XiuXianStateConfig stateConfig : config.getStateConfigMap().values()) {
            builder.addStateConfig(parseXiuXianStateConfig(stateConfig, language));
        }
        for (XiuXianTripodConfig tripodConfig : config.getTripodConfigMap().values()) {
            builder.addTripodConfig(parseXiuXianTripodConfig(tripodConfig));
        }
        for (XiuXianEquipmentConfig equipmentConfig : config.getEquipmentConfigMap().values()) {
            builder.addEquipmentConfig(parseXiuXianEquipmentConfig(equipmentConfig, language));
        }
        for (XiuXianEventConfig eventConfig : config.getEventConfigMap().values()) {
            builder.addEventConfig(parseXiuXianEventConfig(eventConfig, language));
        }
        for (XiuXianNpcConfig npcConfig : config.getNpcConfigMap().values()) {
            builder.addNpcConfig(parseXiuXianNpcConfig(npcConfig, language));
        }
        for (XiuXianMissionConfig missionConfig : config.getMissionConfigMap().values()) {
            builder.addMissionConfig(parseXiuXianMissionConfig(missionConfig, language));
        }
        for (Map<Integer, XiuXianUpgradeConfig> tempMap : config.getUpgradeConfigMap().values()) {
            for (XiuXianUpgradeConfig upgradeConfig : tempMap.values()) {
                builder.addUpgradeConfig(parseXiuXianUpgradeConfig(upgradeConfig));
            }
        }
        for (XiuXianPeakConfig pvpPeakConfig : config.getPeakConfigMap().values()) {
            builder.addPeakConfig(parseXiuXianPeakConfig(pvpPeakConfig, language));
        }
        return builder;
    }

    private static XiuXianProto.XiuXianPvpPlaceConfigTemp.Builder parseXiuXianPvpPlaceConfigTemp(XiuXianPvpPlaceConfig placeConfig, String language) {
        if (placeConfig == null) {
            return null;
        }
        XiuXianProto.XiuXianPvpPlaceConfigTemp.Builder builder = XiuXianProto.XiuXianPvpPlaceConfigTemp.newBuilder();
        builder.setActivityId(placeConfig.getActivityId());
        builder.setId(placeConfig.getId());
        builder.setType(placeConfig.getType());
        builder.setName(ServerLanguageMgr.getContent(placeConfig.getName(), language));
        builder.setLevel(placeConfig.getLevel());
        builder.setRadius(placeConfig.getRadius());
        builder.setSeizeScore(placeConfig.getSeizeScore());
        builder.setSeizeProduce(placeConfig.getSeizeProduce());
        return builder;
    }

    private static XiuXianProto.XiuXianStateConfig.Builder parseXiuXianStateConfig(XiuXianStateConfig stateConfig, String language) {
        if (stateConfig == null) {
            return null;
        }
        XiuXianProto.XiuXianStateConfig.Builder builder = XiuXianProto.XiuXianStateConfig.newBuilder();
        builder.setActivityId(stateConfig.getActivityId());
        builder.setId(stateConfig.getId());
        builder.setStateType(stateConfig.getStateType());
        builder.setClassId(stateConfig.getClassId());
        builder.setName(ServerLanguageMgr.getContent(stateConfig.getName(), language));
        builder.setNeedItem(stateConfig.getNeedItem());
        builder.setConsumeItem(stateConfig.getConsumeItem());
        builder.setNeedXianLi(stateConfig.getNeedXianLi());
        builder.setMinXianLi(stateConfig.getMinXianLi());
        builder.setMaxEffect(stateConfig.getMaxEffect());
        builder.setPracticeSpeed(stateConfig.getPracticeSpeed());
        builder.setAttributeAdd(stateConfig.getAttributeAdd());
        return builder;
    }

    private static XiuXianProto.XiuXianTripodConfig.Builder parseXiuXianTripodConfig(XiuXianTripodConfig tripodConfig) {
        if (tripodConfig == null) {
            return null;
        }
        XiuXianProto.XiuXianTripodConfig.Builder builder = XiuXianProto.XiuXianTripodConfig.newBuilder();
        builder.setActivityId(tripodConfig.getActivityId());
        builder.setLevel(tripodConfig.getLevel());
        builder.setNeedItem(tripodConfig.getNeedItem());
        builder.setStateLimit(tripodConfig.getStateLimit());
        builder.setQualityWeight(tripodConfig.getQualityWeight());
        builder.setTypeRandom(tripodConfig.getTypeRandom());
        builder.setLevelRandom(tripodConfig.getLevelRandom());
        builder.setAdvancedLevelRandom(tripodConfig.getAdvancedLevelRandom());
        builder.setSkillLevelRandom(tripodConfig.getSkillLevelRandom());
        builder.setExtractConsume(tripodConfig.getExtractConsume());
        return builder;
    }

    private static XiuXianProto.XiuXianEquipmentConfig.Builder parseXiuXianEquipmentConfig(XiuXianEquipmentConfig equipmentConfig, String language) {
        if (equipmentConfig == null) {
            return null;
        }
        XiuXianProto.XiuXianEquipmentConfig.Builder builder = XiuXianProto.XiuXianEquipmentConfig.newBuilder();
        builder.setActivityId(equipmentConfig.getActivityId());
        builder.setId(equipmentConfig.getId());
        builder.setIcon(equipmentConfig.getIcon());
        builder.setType(equipmentConfig.getType());
        builder.setQuality(equipmentConfig.getQuality());
        builder.setName(ServerLanguageMgr.getContent(equipmentConfig.getName(), language));
        builder.setDesc(ServerLanguageMgr.getContent(equipmentConfig.getDesc(), language));
        builder.setAttribute(equipmentConfig.getAttribute());
        builder.setAdvancedAttribute(equipmentConfig.getAdvancedAttribute());
        builder.setSkillType(equipmentConfig.getSkillType());
        builder.setSkillParameter(equipmentConfig.getSkillParameter());
        builder.setRecoveryReward(equipmentConfig.getRecoveryReward());
        return builder;
    }

    private static XiuXianProto.XiuXianEventConfig.Builder parseXiuXianEventConfig(XiuXianEventConfig eventConfig, String language) {
        if (eventConfig == null) {
            return null;
        }
        XiuXianProto.XiuXianEventConfig.Builder builder = XiuXianProto.XiuXianEventConfig.newBuilder();
        builder.setActivityId(eventConfig.getActivityId());
        builder.setId(eventConfig.getId());
        builder.setType(eventConfig.getType());
        builder.setEventParameter(eventConfig.getEventParameter());
        builder.setIntroduce(ServerLanguageMgr.getContent(eventConfig.getIntroduce(), language));
        builder.setName(ServerLanguageMgr.getContent(eventConfig.getName(), language));
        builder.setDesc(ServerLanguageMgr.getContent(eventConfig.getDesc(), language));
        builder.setRewards(eventConfig.getRewards());
        builder.setFailRewards(eventConfig.getFailRewards());
        builder.setIcon(eventConfig.getIcon());
        builder.setXianLi(eventConfig.getXianLi());
        return builder;
    }

    private static XiuXianProto.XiuXianNpcConfig.Builder parseXiuXianNpcConfig(XiuXianNpcConfig npcConfig, String language) {
        if (npcConfig == null) {
            return null;
        }
        XiuXianProto.XiuXianNpcConfig.Builder builder = XiuXianProto.XiuXianNpcConfig.newBuilder();
        builder.setActivityId(npcConfig.getActivityId());
        builder.setId(npcConfig.getId());
        builder.setType(npcConfig.getType());
        builder.setSmallType(npcConfig.getSmallType());
        builder.setAttributeType(npcConfig.getAttributeType());
        builder.setIcon(npcConfig.getIcon());
        builder.setName(ServerLanguageMgr.getContent(npcConfig.getName(), language));
        builder.setDesc(ServerLanguageMgr.getContent(npcConfig.getDesc(), language));
        builder.setAttribute(npcConfig.getAttribute());
        builder.setAdvancedAttribute(npcConfig.getAdvancedAttribute());
        return builder;
    }

    private static XiuXianProto.XiuXianMissionConfig.Builder parseXiuXianMissionConfig(XiuXianMissionConfig missionConfig, String language) {
        if (missionConfig == null) {
            return null;
        }
        XiuXianProto.XiuXianMissionConfig.Builder builder = XiuXianProto.XiuXianMissionConfig.newBuilder();
        builder.setActivityId(missionConfig.getActivityId());
        builder.setId(missionConfig.getId());
        builder.setName(ServerLanguageMgr.getContent(missionConfig.getName(), language));
        builder.setDesc(ServerLanguageMgr.getContent(missionConfig.getDesc(), language));
        builder.setStateLimit(missionConfig.getStateLimit());
        builder.setTerrain(missionConfig.getTerrain());
        builder.setType(missionConfig.getType());
        builder.setFloor(missionConfig.getFloor());
        builder.setIcon(missionConfig.getIcon());
        builder.setSubsectionRewards(missionConfig.getSubsectionRewards());
        builder.setBuff(missionConfig.getBuff());
        return builder;
    }

    private static XiuXianProto.XiuXianPeakConfig.Builder parseXiuXianPeakConfig(XiuXianPeakConfig peakConfig, String language) {
        if (peakConfig == null) {
            return null;
        }
        XiuXianProto.XiuXianPeakConfig.Builder builder = XiuXianProto.XiuXianPeakConfig.newBuilder();
        builder.setActivityId(peakConfig.getActivityId());
        builder.setId(peakConfig.getId());
        builder.setName(ServerLanguageMgr.getContent(peakConfig.getName(), language));
        builder.setDesc(ServerLanguageMgr.getContent(peakConfig.getDesc(), language));
        builder.setIcon(peakConfig.getIcon());
        builder.setRoomNum(peakConfig.getRoomNum());
        builder.setPeopleNum(peakConfig.getPeopleNum());
        builder.setAttributeAdd(peakConfig.getAttributeAdd());
        return builder;
    }

    private static XiuXianProto.XiuXianUpgradeConfig.Builder parseXiuXianUpgradeConfig(XiuXianUpgradeConfig upgradeConfig) {
        if (upgradeConfig == null) {
            return null;
        }
        XiuXianProto.XiuXianUpgradeConfig.Builder builder = XiuXianProto.XiuXianUpgradeConfig.newBuilder();
        builder.setActivityId(upgradeConfig.getActivityId());
        builder.setId(upgradeConfig.getId());
        builder.setType(upgradeConfig.getType());
        builder.setLevel(upgradeConfig.getLevel());
        builder.setUpgradeAttr(upgradeConfig.getUpgradeAtt());

        return builder;
    }

    public static XiuXianProto.XiuXianPveUserPropertyTemp.Builder parseXiuXianPveUserPropertyTemp(XiuXianPveUserProperty property) {
        if (property == null) {
            return null;
        }
        XiuXianProto.XiuXianPveUserPropertyTemp.Builder builder = XiuXianProto.XiuXianPveUserPropertyTemp.newBuilder();
        builder.setAttack(property.getAttrMap().getOrDefault(eXiuXianType.BattleAttrType.Atk.getValue(), 0L));
        builder.setHp(property.getAttrMap().getOrDefault(eXiuXianType.BattleAttrType.Hp.getValue(), 0L));
        builder.setAbility(property.getPowerVal());
        for (Map.Entry<Integer, Long> entry : property.getAttrMap().entrySet()) {
            builder.addAttrList(parseXiuXianAttrInfoTemp(entry.getKey(), entry.getValue()));
        }
        return builder;
    }

    public static XiuXianProto.XiuXianPvePropertyForCrossTemp.Builder parseXiuXianPveUserPropertyTemp(XiuXianUserData userData) {
        if (userData == null) {
            return null;
        }
        XiuXianProto.XiuXianPvePropertyForCrossTemp.Builder builder = XiuXianProto.XiuXianPvePropertyForCrossTemp.newBuilder();
        for (Map.Entry<Integer, Long> entry : userData.getProperty().getAttrMap().entrySet()) {
            builder.addAttrList(parseXiuXianAttrInfoTemp(entry.getKey(), entry.getValue()));
        }
        builder.setImageId(userData.getImageId());
        builder.setPracticeSpeed(userData.getPracticeSpeed());
        builder.setStateId(userData.getStateId());
        builder.setPowerVal(userData.getPowerVal());
        return builder;
    }

    public static XiuXianProto.XiuXianAttrInfoTemp.Builder parseXiuXianAttrInfoTemp(int attrId, long attrValue) {
        XiuXianProto.XiuXianAttrInfoTemp.Builder builder = XiuXianProto.XiuXianAttrInfoTemp.newBuilder();
        builder.setAttrId(attrId);
        builder.setAttrValue(attrValue);
        return builder;
    }

    public static XiuXianProto.XiuXianPveChangeDataTemp.Builder parsePveChangeDataTemp(XiuXianUserData userData){
        if(null == userData){
            return null;
        }
        XiuXianProto.XiuXianPveChangeDataTemp.Builder changeBuilder = XiuXianProto.XiuXianPveChangeDataTemp.newBuilder();
        changeBuilder.setLastMeditateTime(userData.getLastMeditateTime());
        changeBuilder.setPowerVal(userData.getPowerVal());
        changeBuilder.setPracticeSpeed(userData.getPracticeSpeed());
        changeBuilder.setPracticeVal(userData.getPracticeVal());
        changeBuilder.setMaxPowerVal(userData.getMaxPowerVal());
        return changeBuilder;
    }

    public static XiuXianProto.BattleObjectTemp.Builder parseBattleObjectTemp(XiuXianBattleData battleData) {
        if (null == battleData) {
            return null;
        }
        XiuXianProto.BattleObjectTemp.Builder builder = XiuXianProto.BattleObjectTemp.newBuilder();
        builder.setId(battleData.getId());
        builder.setNpcId(battleData.getNpcId());
        builder.setMaxHp(battleData.getMaxHp());
        return builder;
    }

    public static XiuXianProto.BattleEventResultInfoTemp.Builder parseBattleEventResultInfoTemp(XiuXianBattleResult battleRes){
        if (null == battleRes) {
            return null;
        }
        XiuXianProto.BattleEventResultInfoTemp.Builder builder = XiuXianProto.BattleEventResultInfoTemp.newBuilder();
        builder.setBattleNum(battleRes.getBattleNum());
        builder.setAttackerType(battleRes.getAttackerType());
        builder.setSkillId(battleRes.getSkillId());
        builder.setWinner(battleRes.getWinner());
        builder.setReward(battleRes.getReward());
        builder.setAtkTime(battleRes.getAtkTime());
        builder.setIsBackAtk(battleRes.getBackAtk());
        // 攻击者信息
        XiuXianProto.BattleInfoTemp.Builder infoBuilder = XiuXianProto.BattleInfoTemp.newBuilder();
        infoBuilder.setId(battleRes.getAttacker().getId());
        infoBuilder.setCurHp(battleRes.getAttacker().getCurHp());
        infoBuilder.setHarmValue(battleRes.getAttacker().getHarmValue());
        infoBuilder.setSuckHp(battleRes.getAttacker().getSuckHp());
        infoBuilder.setIsCrit(battleRes.getAttacker().getCrit());
        infoBuilder.setIsStun(battleRes.getAttacker().getStun());
        infoBuilder.setIsDodge(battleRes.getAttacker().getDodge());
        infoBuilder.setAnger(battleRes.getAttacker().getAnger());
        builder.setAttacker(infoBuilder);
        // 防守者信息
        for(XiuXianBattleInfo infoObj : battleRes.getDefenseList()) {
            infoBuilder = XiuXianProto.BattleInfoTemp.newBuilder();
            infoBuilder.setId(infoObj.getId());
            infoBuilder.setHarmValue(infoObj.getHarmValue());
            infoBuilder.setCurHp(infoObj.getCurHp());
            infoBuilder.setSuckHp(infoObj.getSuckHp());
            infoBuilder.setIsCrit(infoObj.getCrit());
            infoBuilder.setIsStun(infoObj.getStun());
            infoBuilder.setIsDodge(infoObj.getDodge());
            infoBuilder.setAnger(battleRes.getAttacker().getAnger());
            builder.addDefenseList(infoBuilder);
        }
        return builder;
    }
    public static List<XiuXianProto.XiuXianItemMsg> parseXiuXianItemMsgList(Property property) {
        List<XiuXianProto.XiuXianItemMsg> list = new ArrayList<>();
        for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
            XiuXianProto.XiuXianItemMsg.Builder builder = XiuXianProto.XiuXianItemMsg.newBuilder();
            builder.setItemId(entry.getKey());
            builder.setNum(entry.getValue().longValue());
            list.add(builder.build());
        }
        return list;
    }
}
